package com.dituo.esac.mybatis;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import com.dituo.core.enums.event.FireDoorEventTypeEnums;

/**
 * 枚举值转换-将数据库中的code转为desc
 * @author wanghao
 * @date 2019-08-20 10:23
 */
public class EventTypeHandler extends BaseTypeHandler<String> {

	@Override
	public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType)
			throws SQLException {
		// 设置查询参数，一般用不上	
		ps.setInt(i, (null == parameter ? 0 : Integer.parseInt(parameter)));
	}

	@Override
	public String getNullableResult(ResultSet rs, String columnName) throws SQLException {
		// null值为0
		int code = rs.getInt(columnName);
		return FireDoorEventTypeEnums.getDescByCode(code);
	}

	@Override
	public String getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
		// null值为0
		int code = rs.getInt(columnIndex);
		return FireDoorEventTypeEnums.getDescByCode(code);
	}

	@Override
	public String getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
		// null值为0
		int code = cs.getInt(columnIndex);
		return FireDoorEventTypeEnums.getDescByCode(code);
	}

}
